import { AxiosResponse } from 'axios';
import { api } from '../boot/axios';
import { ItemForm, StockItem, StockItemHTReplacement } from './type';

function url(s: string) {
  return ['/api/stock', s.replace('/', ''), ''].join('/');
}

export function getAllStock() {
  return api({
    url: '/api/stock/',
    method: 'get',
  });
}

export function getAllStockCode() {
  return api({
    url: url('get_all_stock_code'),
    method: 'get',
  });
}

export function patchStockItem(id:number,newItemForm: ItemForm ){
  const data = {
    'stock_code': newItemForm.stockCode.toUpperCase(),
    'system': newItemForm.system,
    'workgroup': newItemForm.workgroup,
    'car': newItemForm.car,
    'description': newItemForm.shortDescription,
    'detail_description': newItemForm.detailDescription,
    'date_code': newItemForm.dateCode,
    'sw_ver': newItemForm.swVer,
    'hw_ver': newItemForm.hwVer,
    'asset_id': newItemForm.asset_id,
    'equipment_class': newItemForm.equipmentClass,
    'obsolescence': newItemForm.obsolescence,
    'lv1': newItemForm.lv1,
    'lv2': newItemForm.lv2,
    'lv3': newItemForm.lv3,
    'lv4': newItemForm.lv4,
    'lv5': newItemForm.lv5,
    'parent_stock_item_stock_code': newItemForm.parent_stock_item_stock_code,
    'global_regular_cycle_month': Number(newItemForm.global_regular_cycle_month),
  }
  for (const key in data){
    if (data[key]==''){
      delete data[key]
    }
  }

  data["obsolescence"] = newItemForm.obsolescence;
  
  return api({
    url: `/api/stock/${id}/`,
    method: 'patch',
    data: data
  });
}

export function getStockItem(stock_id: number) {
  return api({
    url: url(`${stock_id}`),
    method: 'get',
  });
}

export function getStockItemByStockCode(stock_code: string) {
  return api({
    url: url('get_stock_item'),
    method: 'post',
    data:{
      stock_code
    }
  });
}

export function updateStockItem(newItemForm:ItemForm):Promise<AxiosResponse<StockItem>> {
  console.log(newItemForm);
  
  return api({
    url: url('update_stock_item'),
    method: 'post',
    data:{
        'stock_code': newItemForm.stockCode.toUpperCase(),
        'system': newItemForm.system,
        'workgroup': newItemForm.workgroup,
        'car': newItemForm.car,
        'short_description': newItemForm.shortDescription,
        'description': newItemForm.detailDescription,
        'date_code': newItemForm.dateCode,
        'sw_ver': newItemForm.swVer,
        'hw_ver': newItemForm.hwVer,
        'asset_id': newItemForm.asset_id,
        'obsolescence': newItemForm.obsolescence,
        'equipment_class': newItemForm.equipmentClass,
        'lv1': newItemForm.lv1,
        'lv2': newItemForm.lv2,
        'lv3': newItemForm.lv3,
        'lv4': newItemForm.lv4,
        'lv5': newItemForm.lv5,
        'parent_stock_item_stock_code': newItemForm.parent_stock_item_stock_code,
        'global_regular_cycle_month': Number(newItemForm.global_regular_cycle_month),
      }
  });
}

export function createStockItem(newItemForm:ItemForm) {
  return api({
    url: url('create_stock_item'),
    method: 'post',
    data:{
        'stock_code': newItemForm.stockCode.toUpperCase(),
        'system': newItemForm.system,
        'workgroup': newItemForm.workgroup,
        'short_description': newItemForm.shortDescription,
        'car': newItemForm.car,
        'description': newItemForm.detailDescription,
        'date_code': newItemForm.dateCode,
        'sw_ver': newItemForm.swVer,
        'hw_ver': newItemForm.hwVer,
        'obsolescence': newItemForm.obsolescence,
        'asset_id': newItemForm.asset_id,
        'equipment_class': newItemForm.equipmentClass,
        'lv1': newItemForm.lv1,
        'lv2': newItemForm.lv2,
        'lv3': newItemForm.lv3,
        'lv4': newItemForm.lv4,
        'lv5': newItemForm.lv5,
        'parent_stock_item_stock_code': newItemForm.parent_stock_item_stock_code,
        'global_regular_cycle_month': Number(newItemForm.global_regular_cycle_month),
      }
  });
}

export function getAllLevel5StockCode() {
  return api({
    url: url('get_all_lv5_stock_code'),
    method: 'post',
  });
}

export function assignChildStock(parent_stock_code:string,stock_code:string) {
  return api({
    url: url('assign_stock_item_child'),
    method: 'post',
    data:{
      parent_stock_item_stock_code:parent_stock_code.toUpperCase(),
      stock_code
    }
  });
}

export function getChildStockItems(parent_stock_code:string) {
  return api({
    url: url('get_child_stock_items'),
    method: 'post',
    data:{
      parent_stock_item_stock_code:parent_stock_code.toUpperCase()
    }
  });
}

export function removeStockItemChild(parent_stock_code:string,stock_code:string, sequence: number) {
  return api({
    url: url('remove_stock_item_child'),
    method: 'post',
    data:{
      parent_stock_item_stock_code:parent_stock_code.toUpperCase(),
      stock_code,
      sequence
    }
  });
}

export function updateChildStockCodeSequence(parent_stock_code:string,sequence_json:{[key:number]:number|string} ) {

  
  return api({
    url: url('reorder_stock_item_child'),
    method: 'post',
    data: {
      sequence_json,
      parent_stock_code:parent_stock_code.toUpperCase()
    },
  });
}

export function deleteStockItemHtreplace(id:number){
  return api({
    url: url('delete_replacement'),
    method: 'post',
    data: {
      id
    },
  });
}

export function getStockItemHtreplace(id:number){
  return api({
    url: url(`get_stock_item_htreplace/${id}`),
    method: 'get',
  });
}

export function createStockItemHtreplace(stock_code:string, component: string, period:number){
  console.log(period);
  
  return api({
    url: url('create_replacement_task'),
    method: 'post',
    data:{
      stock_code,
      component,
      period
    }
  });
}


export function patchStockItemHtreplace(id:number, period:number, component: string){
  return api({
    url: url('update_replacement_task_period'),
    method: 'post',
    data:{
      id,
      period,
      component
    }
  });
}

export function getHTreplacement(stock_code:string){
  return api({
    url: url('get_hard_time_replacement'),
    method: 'post',
    data:{
      stock_code
    }
  });
}

export function getStockItemWithChildNumber(){
  return api({
    url: url('get_stock_item_with_num_of_child'),
    method: 'get',
  });
}

export function getStockItemWithChildNumberFilter(stock_code:string, system:string, description:string){
  return api({
    url: url('get_stock_item_with_num_of_child_filter'),
    method: 'post',
    data:{
      stock_code,
      system,
      description
    }
  });
}

export function softDeleteStockItem(id:number){
  return api({
    url: url('soft_delete_stock_item'),
    method: 'DELETE',
    data:{
      id
    }
  })
}

export function getEdocCompletionRates(){
  return api({
    url: url('get_extra_stock_master_list_edoc_completion_rate'),
    method: 'get',
  });
}

export function getSWVersionCompletion(){
  return api({
    url: url('get_extra_stock_master_list_sw_ver_completion_rate'),
    method: 'get',
  });
}
